﻿package com.tdzx.adapter;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.tdzx.R;
import com.tdzx.entity.SiteInfo;

public class SitesAdapter extends BaseAdapter {
	LayoutInflater factory;
	List<SiteInfo> sites = new ArrayList<SiteInfo>();
	int currentPage = 1;
	int pageSize = 10;
	
	boolean localCity;
	
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;
	

	public SitesAdapter(LayoutInflater factory,boolean localCity,DisplayImageOptions options) {
		this.factory = factory;
		this.localCity = localCity;
		this.options = options;
	}

	@Override
	public int getCount() {
		int size;
		if(sites.size() > pageSize * currentPage) {
			size = pageSize * currentPage;
		} else {
			size = sites.size();
		}
		return size;
	}

	@Override
	public Object getItem(int i) {
		return sites.get(i);
	}

	@Override
	public long getItemId(int i) {
		return sites.get(i).getGoods_id();
	}

	@Override
	public View getView(int i, View view, ViewGroup viewgroup) {
		if(i < pageSize * currentPage) {
			if(view == null) {
				view = factory.inflate(R.layout.list_sites_item, null);
				
			}
			TextView labelName = (TextView) view.findViewById(R.id.labelName);
			final ImageView siteImg = (ImageView) view.findViewById(R.id.merchan_img);
			TextView site_name = (TextView) view.findViewById(R.id.site_name);
			TextView site_price = (TextView) view.findViewById(R.id.site_price);
			TextView site_distance = (TextView) view.findViewById(R.id.site_distance);
			TextView site_id = (TextView) view.findViewById(R.id.site_id);
//			ImageView site_star = (ImageView) view.findViewById(R.id.site_star);
			RatingBar rateBar = (RatingBar) view.findViewById(R.id.rate_bar);
			rateBar.setIsIndicator(true);
			float score = (float) sites.get(i).getScore();
			rateBar.setRating(score);
			Log.i("aaa", score + "------------score---" + i + "-----" + sites.get(i).getVveragePrice());
			/*if(score <= 0) {
				site_star.setImageResource(R.drawable.point0);
			} else if(score > 0 && score <= 1) {
				site_star.setImageResource(R.drawable.point10);
			} else if(score > 1 && score <= 2) {
				site_star.setImageResource(R.drawable.point20);
			} else if(score > 2 && score <= 3) {
				site_star.setImageResource(R.drawable.point30);
			} else if(score > 3 && score <= 4) {
				site_star.setImageResource(R.drawable.point40);
			} else if(score > 4) {
				site_star.setImageResource(R.drawable.point50);
			}*/
			
			site_name.setText(sites.get(i).getGoods_Name());
			if((int)(sites.get(i).getVveragePrice()) != 0) {
				site_price.setText("人均:￥" + getDoubleD(sites.get(i).getVveragePrice())); 
			} else {
				site_price.setText("");
			}
			if(!localCity) {
				double km = Double.valueOf(sites.get(i).getDistance()) / 1000;
				if(km < 1) {
					int dis = (int) (km * 1000);
					site_distance.setText(dis + "M");
				} else {
					double dis = getDoublePoint2(km + "");
					if(dis > 50) {
						site_distance.setText(" >50KM");
					} else {
						site_distance.setText(dis + "KM");
					}
				}
			} else {
				site_distance.setText(">50KM");
			}
			site_id.setText(sites.get(i).getGoods_id() + "");
			labelName.setText(sites.get(i).getLabelName());
//			imageLoader.displayImage(sites.get(i).getGoods_image(), siteImg, options);
			imageLoader.displayImage(sites.get(i).getGoods_image(), siteImg, options, new ImageLoadingListener() {
				
				@Override
				public void onLoadingStarted(String imageUri, View view) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onLoadingFailed(String imageUri, View view,
						FailReason failReason) {
					siteImg.setImageBitmap(null);
					siteImg.setBackgroundDrawable(null);
				}
				
				@Override
				public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
					siteImg.setImageBitmap(null);
					siteImg.setBackgroundDrawable(new BitmapDrawable(loadedImage));
				}
				
				@Override
				public void onLoadingCancelled(String imageUri, View view) {
//					siteImg.setImageBitmap(null);
//					siteImg.setBackgroundDrawable(null);
				}
			});
			/*imageLoader.loadImage(sites.get(i).getGoods_image(), new ImageLoadingListener() {
				
				@Override
				public void onLoadingStarted(String imageUri, View view) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onLoadingFailed(String imageUri, View view,
						FailReason failReason) {
					siteImg.setBackgroundDrawable(null);
				}
				
				@Override
				public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
					siteImg.setBackgroundDrawable(new BitmapDrawable(loadedImage));
				}
				
				@Override
				public void onLoadingCancelled(String imageUri, View view) {
					siteImg.setBackgroundDrawable(null);
				}
			});*/
		}
		return view;
	}
	
	public double getDoublePoint2(String distance) {
		double d_dis = Double.valueOf(distance);
		long ll = Math.round(d_dis * 100);
		d_dis= ll / 100.0;
		return d_dis;
	}
	
	public double getDoubleD(double d) {
		long l = Math.round(d * 100);
		d = l / 100.0;
		return d;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getDatasSize() {
		return sites.size();
	}

	public List<SiteInfo> getSites() {
		return sites;
	}

	public void setSites(List<SiteInfo> sites) {
		this.sites = sites;
	}
	
	
	
}
